import { ResponseData, SUCCESS_CODE } from '@/utils/request';
import { AddRoadItem, RoadItem, SimpleRoadItem, UpdateRoadItem } from './types';
import { RequestData } from '@ant-design/pro-table';
import { message } from 'antd';
import { request } from 'umi';

//添加路段
export async function addRoad(data: AddRoadItem): Promise<ResponseData<any>> {
  const res = await request('/road/add', {
    method: 'POST',
    data: {
      name: data.name,
      address: data.address,
      tenant_id: data.tenant_id
    },
  });

  return { code: res.code, data: res.data, success: true, message: res.message };
}

//编辑路段
export async function updateRoad(data: UpdateRoadItem): Promise<ResponseData<any>> {
  const res = await request('/road/update', {
    method: 'POST',
    data: {
      id: data.id,
      name: data.name,
      address: data.address,
      tenant_id: data.tenant_id
    },
  });
  if (res) return { code: res.code, data: res.data, success: true, message: res.message };
  return res;
}

//删除路段
export async function deleteRoad(data: {
  id: string
}): Promise<ResponseData<any>> {
  const res = await request('/road/delete', {
    method: 'POST',
    data: {
      id: data.id
    },
  });
  if (res) return { code: res.code, data: res.data, success: true, message: res.message };
  return res;
}

//分页查询路段
export async function pageQueryRoad(params: {
  road_name?: string;
  address?: string;
  tenant_id?: string;
  page?: number;
  pageSize?: number;
}): Promise<RequestData<RoadItem>> {
  const res = await request('/road/view', {
    params: {
      page: 1,
      pageSize: 10,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

//分页查询路段简单信息
export async function pageQuerySimpleRoad(params: {
  tenant_id?: string;
  page?: number;
  pageSize?: number;
}): Promise<RequestData<SimpleRoadItem>> {
  const res = await request('/road/view_simple', {
    params: {
      page: 1,
      pageSize: 10,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}